// 产业洞察 - 企业详情
import * as Request from '../request';
import { InnoStrategyMAP } from '@config/map/index';

// 创新战略
export const InnoStrategyAPI = async (company_id) => {
  const url = `/api/company/${company_id}/innovation_score`;
  let res: any;
  try {
    res = await Request.get(url);
  } catch (error) {
    console.log('request error:', url, error);
  }

  const avgPercentArr: any = []; // 平均分  权重算完后，最大值为一百
  const compPercentArr: any = []; // 公司分

  const FixedNumber = (str: string) => {
    const value = str ? str : '';
    const arrNum = String(value).split('.');
    const number = `${arrNum[0] ? arrNum[0] : '0'}.${(arrNum[1] || '000').slice(0, 2)}`;
    return Number(number);
  };

  InnoStrategyMAP.forEach((i) => {
    avgPercentArr.push(FixedNumber(res?.data.score[i.key]?.avg_percent) || 0);
    compPercentArr.push(FixedNumber(res?.data.score[i.key]?.percent) || 0);
  });
  return { avgPercentArr, compPercentArr };
};

// 创新词云
export const WordCloudAPI = async (company_id) => {
  const url = `/api/company/${company_id}/word_cloud`;
  let res: any;
  try {
    res = await Request.get(url);
  } catch (error) {
    console.log('request error:', url, error);
  }

  const nameArr = res.data.source.map((r) => {
    return r.name;
  });

  return nameArr;
};

// 总体信息 && 工商信息
export const CompInfoAPI = async (company_id) => {
  const url = '/api/company/' + company_id;
  let res: any;
  try {
    res = await Request.get(url);
  } catch (error) {
    console.log('request error:', url, error);
  }

  return res?.data ?? {};
};

// 技术布局
export const TechLayoutAPI = async (company_id) => {
  const url = `/api/company/${company_id}/wheel`;
  let res: any;
  try {
    res = await Request.get(url);
  } catch (error) {
    console.log('request error:', url, error);
  }

  //   递归给旭日图最后一层数据添加value字段
  const createValue = (res) => {
    res.forEach((r) => {
      if (r.children) {
        createValue(r.children);
      } else {
        r.value = r.count;
      }
    });
  };

  createValue(res.data);

  return res.data;
};

// 技术构成分析
export const TechAnalysisAPI = async (company_id) => {
  const url = `/api/company/${company_id}/technology`;
  let res: any;
  try {
    res = await Request.get(url);
  } catch (error) {
    console.log('request error:', url, error);
  }
  return res.data;
};
